package member

import (
	"context"

	"myapp/internal/model"
	"myapp/internal/service"

	v1 "myapp/api/member/v1"
)

func (c *ControllerV1) Register(ctx context.Context, req *v1.RegisterReq) (res *v1.RegisterRes, err error) {
	err = service.Member().Register(ctx, model.RegisterInput{
		Username: req.Username,
		Password: req.Password,
		Email:    req.Email,
	})
	return nil, err
}

func (c *ControllerV1) Login(ctx context.Context, req *v1.LoginReq) (res *v1.LoginRes, err error) {
	// 使用JWT模式
	token, err := service.Member().Login(ctx, req.Username, req.Password)
	if err != nil {
		return
	}
	return &v1.LoginRes{Token: token}, nil
}
func (c *ControllerV1) LoginWS(ctx context.Context, req *v1.LoginWSReq) (res *v1.LoginWSRes, err error) {
	// 使用session登录请使用 LoginWS, 此时路由中定义的中间件需要使用CtxWS, 而不是Ctx
	_, err = service.Member().LoginWS(ctx, req.Username, req.Password)
	if err != nil {
		return
	}
	return &v1.LoginWSRes{}, nil
}
